package com.ztgf.message.service;

import com.alibaba.fastjson.JSON;
import com.ztgf.message.model.service.IKafkaProducerService;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.kafka.core.KafkaTemplate;

import javax.annotation.Resource;

/**
 * author: chenyu.wang
 * date: 2020-05-13 14:17
 * Description:
 */
@DubboService
public class KafkaProducers implements IKafkaProducerService {

    private static Logger logger = LoggerFactory.getLogger(KafkaProducers.class);

    @Resource
    private KafkaTemplate<String, Object> kafkaTemplate;

    /**
     * 发送自定义的系统消息
     * @param producerRecord
     */
    public void sendCustomSysMessage(ProducerRecord producerRecord){
        kafkaTemplate.send(producerRecord);
        logger.info("message : {}", JSON.toJSONString(producerRecord));
    }

    /**
     *
     * 发送普通消息
     *
     * @param message
     * @param topic
     */
    @Override
    public void senMessage(String message,String topic){
        kafkaTemplate.send(topic,message);
        logger.info("message : {}", JSON.toJSONString(message));
    }
}
